package westh.ilib.struts.action.manage.researcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.bean.manage.ResearcherBean;
import westh.ilib.struts.action.manage.researcher.ResearcherInfoAction;
import westh.util.YyAction;

public class ResearcherInfoAction extends YyAction{
	// 定义commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ResearcherInfoAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 日志和权限检查，请勿删除
		westh.log.YyLog.addFootprint(request, "动作-显示研究者详细信息");
		String researcherId = request.getParameter("researcherId");
		// 查询数据文件
		String dbName = "researcher.dbxml";
		//将信息封装成json串
		JSONObject inJson = new JSONObject();
		inJson.put("researcherId", researcherId);
		inJson.put("dbName", dbName);
		String inputStr = inJson.toString();
		//json串传入相应的方法
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			// 调用WebService
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getResearcherInfo",inputStr, null);
			outString = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		//解析返回的json串
		JSONObject outJson = JSONObject.fromObject(outString);
		String retCode = outJson.getString("retCode");
		ResearcherBean researcher = new ResearcherBean();
		//判断结果
		if (retCode.equals("0100")) {
			
			researcher.setResearcherId(outJson.getString("researcherId"));
			researcher.setResearcherName(outJson.getString("researcherName"));
			researcher.setResearcherInstitute(outJson.getString("researcherInstitute"));
			researcher.setResearcherHomepage(outJson.getString("researcherHomepage"));
			
			
			request.setAttribute("researcher", researcher);
			return mapping.findForward("succ");
		} else {
			String insertSubjectIndustry = "查看失败";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../researcherListPre.do");
			return mapping.findForward("fail");
		}
	}

}
